package com.ruoyi.pingan.fymodule.thirdpa.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.pingan.config.PayConfig;
import com.ruoyi.pingan.config.TaskExecutePoolConfig;
import com.ruoyi.pingan.exception.CommonException;
import com.ruoyi.pingan.fymodule.account.entity.SxoSafetyCardBinding;
import com.ruoyi.pingan.fymodule.account.entity.SxoSafetyUser;
import com.ruoyi.pingan.fymodule.account.entity.SxoUser;
import com.ruoyi.pingan.fymodule.account.enums.CodeEnum;
import com.ruoyi.pingan.fymodule.account.enums.MemberPropertyEnum;
import com.ruoyi.pingan.fymodule.account.enums.SourceEnum;
import com.ruoyi.pingan.fymodule.account.service.ISxoSafetyCardBindingService;
import com.ruoyi.pingan.fymodule.account.service.ISxoSafetyLogService;
import com.ruoyi.pingan.fymodule.account.service.ISxoSafetyUserService;
import com.ruoyi.pingan.fymodule.account.service.ISxoUserService;
import com.ruoyi.pingan.fymodule.account.util.CommonUtil;
import com.ruoyi.pingan.fymodule.account.util.HttpResult;
import com.ruoyi.pingan.fymodule.account.util.PaPayUtil;
import com.ruoyi.pingan.fymodule.busManage.busSubAcct.entity.busSubAcct;
import com.ruoyi.pingan.fymodule.busManage.busSubAcct.service.BusSubAcctService;
import com.ruoyi.pingan.fymodule.exception.BusinessException;
import com.ruoyi.pingan.fymodule.plugins.entity.SxoPluginsCertificateUserAuth;
import com.ruoyi.pingan.fymodule.plugins.entity.SxoPluginsShop;
import com.ruoyi.pingan.fymodule.plugins.service.ISxoPluginsCertificateUserAuthService;
import com.ruoyi.pingan.fymodule.plugins.service.ISxoPluginsShopService;
import com.ruoyi.pingan.fymodule.thirdpa.api.IBaseClient;
import com.ruoyi.pingan.fymodule.thirdpa.api.impl.*;
import com.ruoyi.pingan.fymodule.thirdpa.model.dto.*;
import com.ruoyi.pingan.fymodule.thirdpa.model.vo.*;
import com.ruoyi.pingan.fymodule.thirdpa.service.PaMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;


@Slf4j
@Service
public class PaMemberServiceImpl extends BasePaServiceImpl implements PaMemberService {

    @Autowired
    private ISxoPluginsShopService sxoPluginsShopService;
    @Autowired
    private ISxoPluginsCertificateUserAuthService sxoPluginsCertificateUserAuthService;
    @Autowired
    private ISxoUserService sxoUserService;
    @Autowired
    private ISxoSafetyUserService sxoSafetyUserService;
    @Autowired
    private ISxoSafetyLogService sxoSafetyLogService;
    @Autowired
    private ISxoSafetyCardBindingService sxoSafetyCardBindingService;
    @Autowired
    private PayConfig payConfig;
    @Autowired
    private TaskExecutePoolConfig taskExecutePoolConfig;

    @Autowired
    private BusSubAcctService busSubAcctService;

    private static final Logger logger = Logger.getLogger(PaMemberServiceImpl.class.getName());



    @Override
    public AutonymOpenCustAcctVo autonymOpenCustAcctInd(AutonymOpenCustAcctDto dto) {
        if (StrUtil.isEmpty(dto.getSource())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "请选择批发商或者商户");
        }
        if (StrUtil.isEmpty(dto.getUserId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "用户ID必须填写");
        }
        SxoSafetyUser user = sxoSafetyUserService.findUser(dto.getUserId(), dto.getSource());
        if (user != null && StrUtil.isNotEmpty(user.getSubAcctNo()) && dto.getFunctionFlag().equals("1")) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前用户已注册；无法重新注册");
        }

        String tranNetMemberCode = "";
        String memberName = "";
        String memberGlobalId = "";
        String indivBusinessFlag = "2";
        String mobile = "";
        String userNickname = "";
        String memberGlobalType = "1";
        String memberProperty = MemberPropertyEnum.SH.getCode();
        JSONObject json = new JSONObject();
        if (StrUtil.isNotEmpty(dto.getSource()) && dto.getSource().equals(SourceEnum.USER.getCode())) {
            SxoUser sxoUser = sxoUserService.getById(dto.getUserId());
            if (sxoUser == null) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "用户Id错误");
            }
            tranNetMemberCode = PaPayUtil.TRAN_NET_MEMBER_CODE_U + sxoUser.getId();
            SxoPluginsCertificateUserAuth userAuth = sxoPluginsCertificateUserAuthService.findAuthByUserId(dto.getUserId(), "33", "");
            if (userAuth == null) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前采购用户身份证未验证");
            }
            SxoPluginsCertificateUserAuth userCompany = sxoPluginsCertificateUserAuthService.findAuthByUserId(dto.getUserId(), "22", "");
            if (userCompany == null) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前采购用户营业执照未验证");
            }
            if (StrUtil.isEmpty(userCompany.getLicenceName())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前采购用户公司名称不为空");
            }
            if (StrUtil.isEmpty(userCompany.getLicenceNumber())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前采购用户社会统一信用代码不为空");
            }
            if (userCompany.getLicenceNumber().length() > 2) {
                indivBusinessFlag = PaPayUtil.getCreditType(userCompany.getLicenceNumber());
            }
//            memberProperty = MemberPropertyEnum.ORD.getCode();
            memberName = userAuth.getLicenceName();
            memberGlobalId = userAuth.getLicenceNumber();
            mobile = sxoUser.getMobile();
            userNickname = StrUtil.isNotEmpty(sxoUser.getNickname()) ? sxoUser.getNickname() : sxoUser.getUsername();
            //个体工商户
            if (indivBusinessFlag.equals("1")) {
                json.put("CompanyName", userCompany.getLicenceName());
                json.put("CompanyGlobalType", "73");
                json.put("CompanyGlobalId", userCompany.getLicenceNumber());
                json.put("ShopId", dto.getTranNetMemberCode());
                json.put("ShopName", userCompany.getLicenceName());
                json.put("RepFlag", "1");
                json.put("ReprName", dto.getMemberName());
                json.put("ReprGlobalType", "1");
                json.put("ReprGlobalId", dto.getMemberGlobalId());
                json.put("Address", sxoUser.getAddress());
            }

        } else {

            QueryWrapper<SxoPluginsShop> queryWrapper = new QueryWrapper<SxoPluginsShop>().checkSqlInjection();
            queryWrapper.lambda().eq(SxoPluginsShop::getUserId, dto.getUserId());


            SxoPluginsShop sxoPluginsShop = sxoPluginsShopService.getOne(queryWrapper);
            if (sxoPluginsShop == null) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "商户Id错误");
            }

            if (StrUtil.isEmpty(sxoPluginsShop.getCompanyName())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "企业名称不为空");
            }
            if (StrUtil.isEmpty(sxoPluginsShop.getCompanyNumber())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "社会统一信用代码不为空");
            }
            if (sxoPluginsShop.getCompanyNumber().length() > 2) {
                indivBusinessFlag = PaPayUtil.getCreditType(sxoPluginsShop.getCompanyNumber());
            }
            tranNetMemberCode = PaPayUtil.TRAN_NET_MEMBER_CODE_S + sxoPluginsShop.getId();
            memberName = sxoPluginsShop.getCompanyName();
            memberGlobalId = sxoPluginsShop.getCompanyNumber();
            memberGlobalType = "73";
//            mobile = "11111111111";
            userNickname = sxoPluginsShop.getName();
            //个体工商户
            if (indivBusinessFlag.equals("1")) {
                json.put("CompanyName", sxoPluginsShop.getCompanyName());
                json.put("CompanyGlobalType", "73");
                json.put("CompanyGlobalId", sxoPluginsShop.getCompanyNumber());
                json.put("ShopId", tranNetMemberCode);
                json.put("ShopName", sxoPluginsShop.getCompanyName());
                json.put("RepFlag", "1");
                json.put("ReprName", sxoPluginsShop.getIdcardName());
                json.put("ReprGlobalType", "1");
                json.put("ReprGlobalId", sxoPluginsShop.getIdcardNumber());
                json.put("Address", sxoPluginsShop.getAddress());
            }
        }
        json.put("TxnCode", "KFEJZB6248");
        if (StrUtil.isNotEmpty(dto.getFunctionFlag())) {
            json.put("FunctionFlag", dto.getFunctionFlag());
        } else {
            json.put("FunctionFlag", "1");
        }

        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("TranNetMemberCode", dto.getTranNetMemberCode());
        json.put("MemberGlobalType", dto.getMemberGlobalType());
        json.put("MemberGlobalId", dto.getMemberGlobalId());
        json.put("MemberName", dto.getMemberName());

        json.put("UserNickname", userNickname);
//        json.put("Mobile", mobile);
        json.put("MemberProperty", dto.getMemberProperty());
        json.put("IndivBusinessFlag", indivBusinessFlag);


        return buildAcc(dto, json, tranNetMemberCode, user);
    }

    private AutonymOpenCustAcctVo buildAcc(AutonymOpenCustAcctDto dto, JSONObject json, String tranNetMemberCode, SxoSafetyUser safetyUser) {
        AutonymOpenCustAcctVo vo = new AutonymOpenCustAcctVo();
        BeanUtils.copyProperties(dto, vo);
        IBaseClient baseClient = new AutonymOpenCustAcctApi(sxoSafetyLogService, taskExecutePoolConfig);
        logger.info("6248调平安入参："+json);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        logger.info("6248调平安出参："+obj);
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);
        String subAcctNo = extenddata.get("SubAcctNo") != null ? extenddata.getString("SubAcctNo") : "";
        if (obj.get(PaPayUtil.code) != null && !obj.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            if (StrUtil.isEmpty(subAcctNo)) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
            }
        }

        if (StrUtil.isNotEmpty(subAcctNo)) {
            if (safetyUser == null) {
                SxoSafetyUser user = new SxoSafetyUser();
                user.setSubAcctNo(subAcctNo);
                user.setTranNetMemberCode(tranNetMemberCode);
                user.setUserId(dto.getUserId());
                user.setSource(dto.getSource());
                user.setStatus(0);
                user.setReinStatus(0);

                user.setMemberName(json.getString("MemberName"));
                user.setMemberGlobalType(json.getString("MemberGlobalType"));
                user.setMemberProperty(json.getString("MemberProperty"));
                user.setMemberGlobalId(json.getString("MemberGlobalId"));
                user.setMobile(json.getString("Mobile"));

                user.setIndivBusinessFlag(json.getString("IndivBusinessFlag"));
                user.setCompanyName(json.getString("CompanyName"));
                user.setCompanyGlobalType(json.getString("CompanyGlobalType"));
                user.setCompanyGlobalId(json.getString("CompanyGlobalId"));

                user.setShopId(json.getString("ShopId"));
                user.setShopName(json.getString("ShopName"));

                user.setRepFlag(json.getString("RepFlag"));
                user.setReprName(json.getString("ReprName"));
                user.setReprGlobalType(json.getString("ReprGlobalType"));
                user.setReprGlobalId(json.getString("ReprGlobalId"));
                user.setCnsmrSeqNo(json.getString(PaPayUtil.CNSMR_SEQ_NO));

                String data = DateUtil.now();
                user.setAddTime(data);
                user.setUpdTime(data);
                sxoSafetyUserService.save(user);
            } else {
                SxoSafetyUser mod = new SxoSafetyUser();
                mod.setId(safetyUser.getId());
                mod.setSubAcctNo(subAcctNo);
                mod.setUpdTime(DateUtil.now());
                if (json.getString("FunctionFlag") != null && json.getString("FunctionFlag").equals("2")) {
                    mod.setReinStatus(2);
//                    mod.setCancellationTime(DateUtil.now());
                    //mod.setSubAcctNo("");
                }
                sxoSafetyUserService.updateById(mod);
            }
        }
        vo.setSubAcctNo(subAcctNo);
        return vo;
    }

    @Override
    public RegisterBehaviorRecordVo registerBehavior(RegisterBehaviorRecordDto dto) throws SocketException {
        SxoSafetyUser safetyUser = sxoSafetyUserService.findUser(dto.getUserId(), dto.getSource());
        if (safetyUser == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前没有会员入驻，请先入驻");
        }
        if (StrUtil.isEmpty(safetyUser.getSubAcctNo())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前用户入，子账号错误");
        }
        if (StrUtil.isEmpty(safetyUser.getTranNetMemberCode())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前用户会员错误");
        }
        SxoSafetyCardBinding card = sxoSafetyCardBindingService.findCard(safetyUser.getUserId(), safetyUser.getSource());

        if (safetyUser.getReinStatus() != null && safetyUser.getReinStatus().equals(1)) {
            if (card != null && card.getBindFlag() != null && card.getBindFlag().equals("1")) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前用户已登记成功；无法重新登记");
            }
        }

        RegisterBehaviorRecordVo vo = new RegisterBehaviorRecordVo();
        BeanUtils.copyProperties(dto, vo);
        vo.setSubAcctNo(safetyUser.getSubAcctNo());

        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6244");
        json.put("FunctionFlag", "1");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("SubAcctNo", safetyUser.getSubAcctNo());
        json.put("TranNetMemberCode", safetyUser.getTranNetMemberCode());
        json.put("OpClickTime", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
        json.put("IpAddress", CommonUtil.getLocalIpAddress());
        String macAddress = CommonUtil.getLocalMacAddress();
        if("".equals(macAddress)){
            macAddress = CommonUtil.getLinuxMacAddress();
        }
        json.put("MacAddress", macAddress);
        json.put("SignChannel", "2");


        IBaseClient baseClient = new RegisterBehaviorRecordApi(sxoSafetyLogService, taskExecutePoolConfig);
        logger.info("6244调平安入参："+json);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSONObject.parseObject(httpResult.getBody());
        logger.info("6244调平安出参："+obj);
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);
        if (extenddata.get(PaPayUtil.TXN_RETURN_CODE) != null && !extenddata.getString(PaPayUtil.TXN_RETURN_CODE).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }
        vo.setReinStatus(1);

        SxoSafetyUser mod = new SxoSafetyUser();
        mod.setId(safetyUser.getId());
        mod.setReinStatus(vo.getReinStatus());
        mod.setUpdTime(DateUtil.now());
//        mod.setCnsmrSeqNo(extenddata.getString(PaPayUtil.CNSMR_SEQ_NO));
        sxoSafetyUserService.updateById(mod);

        if (card != null) {
            SxoSafetyCardBinding modCard = new SxoSafetyCardBinding();
            modCard.setId(card.getId());
//            modCard.setBindFlag(vo.getReinStatus().toString());
            modCard.setUpdTime(DateUtil.now());
            sxoSafetyCardBindingService.updateById(modCard);
        }
        return vo;
    }

    @Override
    public BindSmallAmountWithCheckCorpVo bindSmallAmountWithCheckCorp(BindSmallAmountWithCheckCorpDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }
        SxoSafetyCardBinding card = sxoSafetyCardBindingService.getById(dto.getId());
        if (card == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "ID错误");
        }

        SxoSafetyUser safetyUser = sxoSafetyUserService.findUser(card.getUserId(), card.getSource());
        if (safetyUser == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前没有会员入驻，请先入驻");
        }

        String memberName = card.getMemberName();
        String memberGlobalType = card.getMemberGlobalType();
        String memberGlobalId = card.getMemberGlobalId();


        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6240");
        //必填项
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("SubAcctNo", safetyUser.getSubAcctNo());
        json.put("TranNetMemberCode", card.getTranNetMemberCode());
        //会员信息
        json.put("MemberName", memberName);
        json.put("MemberGlobalType", memberGlobalType);
        json.put("MemberGlobalId", memberGlobalId);
        json.put("MemberAcctNo", card.getMemberAcctNo());
        json.put("BankType", card.getBankType());
        json.put("AcctOpenBranchName", card.getAcctOpenBranchName());
        json.put("Mobile", card.getMobile());

        json.put("CnapsBranchId", card.getCnapsBranchId());
        json.put("EiconBankBranchId", card.getEiconBankBranchId());
        //个体工商户
        json.put("IndivBusinessFlag", card.getIndivBusinessFlag().toString());
        if (card.getIndivBusinessFlag() != null && card.getIndivBusinessFlag().equals(1)) {
            json.put("CompanyName", card.getCompanyName());
            json.put("CompanyGlobalType", card.getCompanyGlobalType());
            json.put("CompanyGlobalId", card.getCompanyGlobalId());
        }
        json.put("ShopId", card.getShopId());
        json.put("ShopName", card.getShopName());

        //经办人
        json.put("AgencyClientFlag", card.getAgencyClientFlag().toString());
        if (card.getAgencyClientFlag() != null && card.getAgencyClientFlag().equals(1)) {
            json.put("AgencyClientName", card.getAgencyClientName());
            json.put("AgencyClientGlobalType", card.getAgencyClientGlobalType().toString());
            json.put("AgencyClientGlobalId", card.getAgencyClientGlobalId());
            json.put("AgencyClientMobile", card.getAgencyClientMobile());
        }
        //法人信息
        json.put("RepFlag", card.getRepFlag().toString());
        json.put("ReprName", card.getReprName());
        json.put("ReprGlobalType", card.getReprGlobalType());
        json.put("ReprGlobalId", card.getReprGlobalId());

        json.put("ReservedMsg", card.getReservedMsg());

        BindSmallAmountWithCheckCorpVo vo = new BindSmallAmountWithCheckCorpVo();
        vo.setUserId(card.getUserId());
        vo.setSource(card.getSource());

        IBaseClient baseClient = new BindSmallAmountWithCheckCorpApi(sxoSafetyLogService, taskExecutePoolConfig);
        logger.info("6240调平安入参："+json);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        logger.info("6240调平安出参："+obj);
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        if (obj.get(PaPayUtil.code) != null && !obj.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }

        vo.setFrontSeqNo(extenddata.getString(PaPayUtil.CNSMR_SEQ_NO));
        card.setCnsmrSeqNo(extenddata.getString(PaPayUtil.CNSMR_SEQ_NO));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        card.setTranDate(sdf.format(new Date()));
        sxoSafetyCardBindingService.updateById(card);
        return vo;
    }

    @Override
    public BindSmallAmountWithCheckCorpVo checkAmountWithCorp(BindSmallAmountWithCheckCorpDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }
        if (StrUtil.isEmpty(dto.getAuthAmt())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "鉴权金额必须填写");
        }
        if (StrUtil.isEmpty(dto.getOrderNo())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "指令号必须填写");
        }
        SxoSafetyCardBinding card = sxoSafetyCardBindingService.getById(dto.getId());
        if (card == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "ID错误");
        }

        SxoSafetyUser safetyUser = sxoSafetyUserService.findUser(card.getUserId(), card.getSource());
        if (safetyUser == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前没有会员入驻，请先入驻");
        }


        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6241");

        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("TranNetMemberCode", card.getTranNetMemberCode());
        json.put("SubAcctNo", safetyUser.getSubAcctNo());
        json.put("TakeCashAcctNo", card.getMemberAcctNo());
        json.put("AuthAmt", dto.getAuthAmt());
        json.put("OrderNo", dto.getOrderNo());
        json.put("Ccy", "RMB");


        BindSmallAmountWithCheckCorpVo vo = new BindSmallAmountWithCheckCorpVo();
        vo.setUserId(card.getUserId());
        vo.setSource(card.getSource());
        vo.setBindFlag("1");

        IBaseClient baseClient = new CheckAmountWithCorpApi(sxoSafetyLogService, taskExecutePoolConfig);
        logger.info("6241调平安入参："+json);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        logger.info("6241调平安出参："+obj);
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        SxoSafetyCardBinding mod = new SxoSafetyCardBinding();
        mod.setId(card.getId());
        mod.setFrontSeqNo(extenddata.getString("FrontSeqNo"));
        mod.setTranDate(DateUtil.format(new Date(), "yyyyMMdd"));

        if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            vo.setErrorMessage(extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
            vo.setBindFlag("-1");
            return vo;
        }
        mod.setBindFlag("1");
        mod.setUpdTime(DateUtil.now());
        sxoSafetyCardBindingService.updateById(mod);
        vo.setFrontSeqNo(extenddata.getString("FrontSeqNo"));
        return vo;
    }

    @Override
    public UnbindRelateAcctVo unbindRelateAcct(UnbindRelateAcctDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }

        SxoSafetyCardBinding card = sxoSafetyCardBindingService.getById(dto.getId());
        if (card == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "ID错误");
        }
        LambdaQueryWrapper<busSubAcct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(busSubAcct::getUserId,card.getUserId());
        queryWrapper.eq(busSubAcct::getTranNetMemberCode,card.getTranNetMemberCode());
        busSubAcct busSubAccts = busSubAcctService.getOne(queryWrapper);
        if (busSubAccts == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前没有会员入驻，请先入驻");
        }


        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6065");
        json.put("FunctionFlag", "1");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("TranNetMemberCode", card.getTranNetMemberCode());
        json.put("SubAcctNo", busSubAccts.getSubAcctNo());
        json.put("MemberAcctNo", card.getMemberAcctNo());


        UnbindRelateAcctVo vo = new UnbindRelateAcctVo();

        IBaseClient baseClient = new UnbindRelateAcctApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }
        if (extenddata.get(PaPayUtil.TXN_RETURN_CODE) != null && !extenddata.getString(PaPayUtil.TXN_RETURN_CODE).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }

        SxoSafetyCardBinding mod = new SxoSafetyCardBinding();
        mod.setId(card.getId());
        mod.setBindFlag("0");
        mod.setCnsmrSeqNo("");
        mod.setTranDate("");
        mod.setUpdTime(DateUtil.now());
        sxoSafetyCardBindingService.updateById(mod);
        vo.setFrontSeqNo(extenddata.getString("FrontSeqNo"));
        return vo;
    }

    @Override
    public SmallAmountTransferQueryVo smallAmountTransferQuery(UnbindRelateAcctDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }

        SxoSafetyCardBinding card = sxoSafetyCardBindingService.getById(dto.getId());
        if (card == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "ID错误");
        }


        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6061");
        String cnsmrSeqNo = card.getCnsmrSeqNo();
        String tranDate = card.getTranDate();
        if(cnsmrSeqNo==null){
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "验证未通过，原交易流水号为空");

        }
        if(tranDate==null){
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "验证未通过，交易日期为空");

        }
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo()==null?"":payConfig.getFundSummaryAcctNo());
        json.put("OldTranSeqNo", card.getCnsmrSeqNo()==null?"":card.getCnsmrSeqNo());
        json.put("TranDate", card.getTranDate()==null?"":card.getTranDate());


        IBaseClient baseClient = new SmallAmountTransferQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
        logger.info("6061调平安入参："+json);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        logger.info("6061调平安出参："+obj);
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }
        if (extenddata.get(PaPayUtil.TXN_RETURN_CODE) != null && !extenddata.getString(PaPayUtil.TXN_RETURN_CODE).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }

        return SmallAmountTransferQueryVo.builder()
                .returnStatu(extenddata.getString("ReturnStatu"))
                .returnMsg(extenddata.getString("ReturnMsg"))
                .build();
    }

    @Override
    public Object eJZBCustInformationQuery(EJZBCustInformationQueryDto dto) {
        if (StrUtil.isEmpty(dto.getThirdCustId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }


        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6324");
        json.put("CustAcctId", dto.getCustAcctId());
        json.put("ThirdCustId", dto.getThirdCustId());


        IBaseClient baseClient = new EJZBCustInformationQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }
        return buildResult(extenddata);
    }

    @Override
    public MemberBindQueryVo memberBindQuery(MemberBindQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6098");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("QueryFlag", dto.getQueryFlag());
        json.put("SubAcctNo", dto.getSubAcctNo());
        json.put("PageNum", dto.getPageNum());


        IBaseClient baseClient = new MemberBindQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new CommonException(httpResult.getError(), CodeEnum.SERVER_ERROR.getCode());

        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            throw new CommonException(extenddata.getString(PaPayUtil.TXN_RETURN_MSG), CodeEnum.SERVER_ERROR.getCode());

        }
        if (extenddata.get(PaPayUtil.TXN_RETURN_CODE) != null && !extenddata.getString(PaPayUtil.TXN_RETURN_CODE).equals(PaPayUtil.code_000000)) {
            throw new CommonException(extenddata.getString(PaPayUtil.TXN_RETURN_MSG), CodeEnum.SERVER_ERROR.getCode());
        }

        MemberBindQueryVo resultVo =          JSONObject.parseObject(obj.toJSONString(), MemberBindQueryVo.class);

        return resultVo;

    }

    @Override
    public Object queryCustAcctIdByThirdCustId(String tranNetMemberCode) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6092");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("TranNetMemberCode", tranNetMemberCode);


        IBaseClient baseClient = new QueryCustAcctIdByThirdCustIdApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }
        return buildResult(extenddata);
    }

    @Override
    public BaseVo mntMbrBindRelateAcctBankCode(MntMbrBindRelateAcctBankCodeDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }
        if (StrUtil.isEmpty(dto.getMemberBindAcctNo())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "银行卡号或者对公账号必须填写");
        }
        if (StrUtil.isEmpty(dto.getAcctOpenBranchName())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "开户行名称必须填写");
        }
        if (StrUtil.isEmpty(dto.getCnapsBranchId()) && StrUtil.isEmpty(dto.getEiconBankBranchId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "大小额行号或者超级网银行号必须填写");
        }


        SxoSafetyCardBinding card = sxoSafetyCardBindingService.getById(dto.getId());
        if (card == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "ID错误");
        }
        SxoSafetyUser safetyUser = sxoSafetyUserService.findUser(card.getUserId(), card.getSource());
        if (safetyUser == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前没有会员入驻，请先入驻");
        }

        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6138");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("SubAcctNo", safetyUser.getSubAcctNo());
        json.put("MemberBindAcctNo", dto.getMemberBindAcctNo());
        json.put("AcctOpenBranchName", dto.getAcctOpenBranchName());
        json.put("CnapsBranchId", dto.getCnapsBranchId());
        json.put("EiconBankBranchId", dto.getEiconBankBranchId());


        IBaseClient baseClient = new MntMbrBindRelateAcctBankCodeApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject extenddata = handleError(httpResult);

        SxoSafetyCardBinding mod = new SxoSafetyCardBinding();
        mod.setId(card.getId());
        mod.setUpdTime(DateUtil.now());
        mod.setMemberAcctNo(dto.getMemberBindAcctNo());
        mod.setAcctOpenBranchName(dto.getAcctOpenBranchName());
        mod.setCnapsBranchId(dto.getCnapsBranchId());
        mod.setEiconBankBranchId(dto.getEiconBankBranchId());
        sxoSafetyCardBindingService.updateById(mod);
        BaseVo vo = new BaseVo();
        vo.setReservedMsg(extenddata.getString("ReservedMsg"));
        return vo;
    }

    @Override
    public ApplyForChangeOfCellPhoneNumVo applyForChangeOfCellPhoneNum(ApplyForChangeOfCellPhoneNumDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }
        if (StrUtil.isEmpty(dto.getModifyType())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "修改方式必须填写");
        }
        if (StrUtil.isEmpty(dto.getNewMobile())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "新手机号码必须填写");
        }
        if (dto.getModifyType().equals("2") && StrUtil.isEmpty(dto.getBankCardNo())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "银行卡号必须填写");
        }


        SxoSafetyCardBinding card = sxoSafetyCardBindingService.getById(dto.getId());
        if (card == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "ID错误");
        }
        SxoSafetyUser safetyUser = sxoSafetyUserService.findUser(card.getUserId(), card.getSource());
        if (safetyUser == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前没有会员入驻，请先入驻");
        }

        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6083");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("TranNetMemberCode", safetyUser.getTranNetMemberCode());
        json.put("SubAcctNo", safetyUser.getSubAcctNo());
        json.put("ModifyType", dto.getModifyType());
        json.put("NewMobile", dto.getNewMobile());
        json.put("MobileLocation", dto.getMobileLocation());
        json.put("BankCardNo", dto.getBankCardNo());


        IBaseClient baseClient = new ApplyForChangeOfCellPhoneNumApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject extenddata = handleError(httpResult);

        SxoSafetyCardBinding mod = new SxoSafetyCardBinding();
        mod.setId(card.getId());
        mod.setUpdTime(DateUtil.now());
        mod.setMobile(dto.getNewMobile());
        sxoSafetyCardBindingService.updateById(mod);
        ApplyForChangeOfCellPhoneNumVo vo = new ApplyForChangeOfCellPhoneNumVo();
        vo.setReceiveMobile(extenddata.getString("ReceiveMobile"));
        vo.setMessageOrderNo(extenddata.getString("MessageOrderNo"));
        vo.setReservedMsg(extenddata.getString("ReservedMsg"));
        return vo;
    }

    @Override
    public Object custAcctIdBalanceQuery(CustAcctIdBalanceQueryDto dto) {

        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6010");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("SubAcctNo", dto.getSubAcctNo());
        json.put("QueryFlag", dto.getQueryFlag());
        json.put("PageNum", dto.getPageNum());


        IBaseClient baseClient = new CustAcctIdBalanceQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }
        return buildResult(extenddata);
    }

    @Override
    public BindUnionPayWithCheckCorpVo bindUnionPayWithCheckCorp(BindUnionPayWithCheckCorpDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }
        SxoSafetyCardBinding card = sxoSafetyCardBindingService.getById(dto.getId());
        if (card == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "ID错误");
        }

        SxoSafetyUser safetyUser = sxoSafetyUserService.findUser(card.getUserId(), card.getSource());
        if (safetyUser == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前没有会员入驻，请先入驻");
        }
        String memberName = card.getMemberName();
        String memberGlobalType = card.getMemberGlobalType();
        String memberGlobalId = card.getMemberGlobalId();

        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6238");
        //必填项
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("SubAcctNo", safetyUser.getSubAcctNo());
        json.put("TranNetMemberCode", card.getTranNetMemberCode());
        //会员信息
        json.put("MemberName", memberName);
        json.put("MemberGlobalType", memberGlobalType);
        json.put("MemberGlobalId", memberGlobalId);
        json.put("MemberAcctNo", card.getMemberAcctNo());
        json.put("BankType", card.getBankType());
        json.put("AcctOpenBranchName", card.getAcctOpenBranchName());


        json.put("CnapsBranchId", card.getCnapsBranchId());
        json.put("EiconBankBranchId", card.getEiconBankBranchId());
        json.put("Mobile", card.getMobile());
        //个体工商户
        json.put("IndivBusinessFlag", card.getIndivBusinessFlag());
        if (card.getIndivBusinessFlag() != null && card.getIndivBusinessFlag().equals(1)) {
            json.put("CompanyName", card.getCompanyName());
            json.put("CompanyGlobalType", card.getCompanyGlobalType());
            json.put("CompanyGlobalId", card.getCompanyGlobalId());
        }
        json.put("ShopId", card.getShopId());
        json.put("ShopName", card.getShopName());

        //法人信息
        json.put("RepFlag", card.getRepFlag());
        json.put("ReprName", card.getReprName());
        json.put("ReprGlobalType", card.getReprGlobalType());
        json.put("ReprGlobalId", card.getReprGlobalId());

        json.put("ReservedMsg", card.getReservedMsg());
        BindUnionPayWithCheckCorpVo vo = new BindUnionPayWithCheckCorpVo();
        vo.setUserId(card.getUserId());
        vo.setSource(card.getSource());

        IBaseClient baseClient = new BindUnionPayWithCheckCorpApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        if (obj.get(PaPayUtil.code) != null && !obj.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
        }
        return vo;
    }

    @Override
    public CheckMsgCodeWithCorpVo checkMsgCodeWithCorp(CheckMsgCodeWithCorpDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }
        if (StrUtil.isEmpty(dto.getMessageCheckCode())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "短信验证码必须填写");
        }

        SxoSafetyCardBinding card = sxoSafetyCardBindingService.getById(dto.getId());
        if (card == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "ID错误");
        }

        SxoSafetyUser safetyUser = sxoSafetyUserService.findUser(card.getUserId(), card.getSource());
        if (safetyUser == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前没有会员入驻，请先入驻");
        }


        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6239");

        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("TranNetMemberCode", card.getTranNetMemberCode());
        json.put("SubAcctNo", safetyUser.getSubAcctNo());
        json.put("MemberAcctNo", card.getMemberAcctNo());
        json.put("MessageCheckCode", dto.getMessageCheckCode());


        CheckMsgCodeWithCorpVo vo = new CheckMsgCodeWithCorpVo();


        IBaseClient baseClient = new CheckMsgCodeWithCorpApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        SxoSafetyCardBinding mod = new SxoSafetyCardBinding();
        mod.setId(card.getId());
        if (StrUtil.isEmpty(card.getCnsmrSeqNo())) {
            mod.setCnsmrSeqNo(extenddata.getString("FrontSeqNo"));
            mod.setTranDate(DateUtil.format(new Date(), "yyyyMMdd"));
        }

        mod.setUpdTime(DateUtil.now());
        sxoSafetyCardBindingService.updateById(mod);
        vo.setFrontSeqNo(extenddata.getString("FrontSeqNo"));
        return vo;
    }

    @Override
    public AutonymOpenCustAcctVo autonymOpenCustAcctPerson(AutonymOpenCustAcctDto dto) {
        if (StrUtil.isEmpty(dto.getSource())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "请选择批发商或者商户");
        }
        if (StrUtil.isEmpty(dto.getUserId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "用户ID必须填写");
        }
        SxoSafetyUser user = sxoSafetyUserService.findUser(dto.getUserId(), dto.getSource());
        if (user != null && StrUtil.isNotEmpty(user.getSubAcctNo()) && dto.getFunctionFlag().equals("1")) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前用户已注册；无法重新注册");
        }

        String tranNetMemberCode = "";
        String memberName = "";
        String memberGlobalId = "";
        String mobile = "";
        String userNickname = "";
        JSONObject json = new JSONObject();
        if (StrUtil.isNotEmpty(dto.getSource()) && dto.getSource().equals(SourceEnum.USER.getCode())) {
            SxoUser sxoUser = sxoUserService.getById(dto.getUserId());
            if (sxoUser == null) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "用户Id错误");
            }
            tranNetMemberCode = PaPayUtil.TRAN_NET_MEMBER_CODE_U + sxoUser.getId();
            SxoPluginsCertificateUserAuth userAuth = sxoPluginsCertificateUserAuthService.findAuthByUserId(dto.getUserId(), "33", "");
            if (userAuth == null) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前采购用户身份证未验证");
            }
            SxoPluginsCertificateUserAuth userCompany = sxoPluginsCertificateUserAuthService.findAuthByUserId(dto.getUserId(), "22", "");
            if (userCompany == null) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前采购用户营业执照未验证");
            }
            if (StrUtil.isEmpty(userCompany.getLicenceName())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前采购用户公司名称不为空");
            }
            if (StrUtil.isEmpty(userCompany.getLicenceNumber())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前采购用户社会统一信用代码不为空");
            }

            memberName = userAuth.getLicenceName();
            memberGlobalId = userAuth.getLicenceNumber();
            mobile = sxoUser.getMobile();
            userNickname = StrUtil.isNotEmpty(sxoUser.getNickname()) ? sxoUser.getNickname() : sxoUser.getUsername();
            //个体工商户
            json.put("RepFlag", "1");
            json.put("ReprName", memberName);
            json.put("ReprGlobalType", "1");
            json.put("ReprGlobalId", memberGlobalId);
            json.put("Address", sxoUser.getAddress());


        } else {
            SxoPluginsShop sxoPluginsShop = sxoPluginsShopService.getById(dto.getUserId());
            if (sxoPluginsShop == null) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "商户Id错误");
            }

            if (StrUtil.isEmpty(sxoPluginsShop.getIdcardName())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "真实姓名不为空");
            }
            if (StrUtil.isEmpty(sxoPluginsShop.getIdcardNumber())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "法人身份证号不为空");
            }
            if (StrUtil.isEmpty(sxoPluginsShop.getCompanyName())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "企业名称不为空");
            }
            if (StrUtil.isEmpty(sxoPluginsShop.getCompanyNumber())) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "社会统一信用代码不为空");
            }

            tranNetMemberCode = PaPayUtil.TRAN_NET_MEMBER_CODE_S + sxoPluginsShop.getId();
            memberName = sxoPluginsShop.getCompanyName();
            memberGlobalId = sxoPluginsShop.getCompanyNumber();
//            mobile = "11111111111";
            userNickname = sxoPluginsShop.getName();
            //个体工商户
            json.put("RepFlag", "1");
            json.put("ReprName", sxoPluginsShop.getIdcardName());
            json.put("ReprGlobalType", "1");
            json.put("ReprGlobalId", sxoPluginsShop.getIdcardNumber());
            json.put("Address", sxoPluginsShop.getAddress());

        }
        json.put("TxnCode", "KFEJZB6248");
        if (StrUtil.isNotEmpty(dto.getFunctionFlag())) {
            json.put("FunctionFlag", dto.getFunctionFlag());
        } else {
            json.put("FunctionFlag", "1");
        }

        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("TranNetMemberCode", tranNetMemberCode);
        json.put("MemberGlobalType", "1");
        json.put("MemberGlobalId", memberGlobalId);
        json.put("MemberName", memberName);

        json.put("UserNickname", userNickname);
//        json.put("Mobile", mobile);
        json.put("MemberProperty", "SH");
        json.put("IndivBusinessFlag", "2");


        return buildAcc(dto, json, tranNetMemberCode, user);
    }

    @Override
    public CrowdAcountMsgQueryVo queryCrowdAcountMsg(CrowdAcountMsgQueryDto dto) {

        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6292");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("OldPayBussOrderNo", dto.getOldPayBussOrderNo());



        IBaseClient baseClient = new QueryCrowdAcountMsgApi(sxoSafetyLogService, taskExecutePoolConfig);
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new CommonException(httpResult.getError(), CodeEnum.SERVER_ERROR.getCode());

        }
        JSONObject obj = JSON.parseObject(httpResult.getBody());
        JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

        if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
            throw new CommonException(extenddata.getString(PaPayUtil.TXN_RETURN_MSG), CodeEnum.SERVER_ERROR.getCode());

        }
        if (extenddata.get(PaPayUtil.TXN_RETURN_CODE) != null && !extenddata.getString(PaPayUtil.TXN_RETURN_CODE).equals(PaPayUtil.code_000000)) {
            throw new CommonException(extenddata.getString(PaPayUtil.TXN_RETURN_MSG), CodeEnum.SERVER_ERROR.getCode());
        }

        CrowdAcountMsgQueryVo resultVo = JSONObject.parseObject(obj.toJSONString(), CrowdAcountMsgQueryVo.class);

        return resultVo;
    }


}
